package com.study.talk.app.base;

import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.study.talk.R;
import com.study.talk.app.App;
import com.study.talk.app.RegisterActivity;
import com.study.talk.net.NetApi;
import com.study.talk.net.OkHttp;
import com.study.talk.util.ToastUtil;

public class BaseFragment extends Fragment {

    public Uri uri;

    public boolean checkInput(EditText textView, String toast) {
        if (TextUtils.isEmpty(editTrim(textView))) {
            ToastUtil.showToast(getContext(), toast);
            return true;
        }
        return false;
    }

    public boolean checkInput(EditText textView, String toast, Integer minLength, String Toast) {
        if (TextUtils.isEmpty(editTrim(textView))) {
            ToastUtil.showToast(getContext(), toast);
            return true;
        }
        return false;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.e("onViewCreated", "onViewCreated: "+view.getBackground());
    }

    public void setText(EditText editText, String text) {
        editText.setText(text);
    }


    //输入框去空格
    public String editTrim(EditText text) {
        return text.getText().toString().trim();
    }

    //显示底部选择图像弹窗，由用户选择拍照或选择图像
    public void showSelectPhotoDialog(ActivityResultLauncher<PickVisualMediaRequest> selectPhotoLauncher, ActivityResultLauncher<Uri> takePhotoLauncher) {
        BottomSheetDialog dialog = new BottomSheetDialog(requireActivity());

        View bottomDialogView = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_bottom, null);
        TextView takePhoto = bottomDialogView.findViewById(R.id.take_photo);
        TextView selectPhoto = bottomDialogView.findViewById(R.id.select_photo);
        TextView cancel = bottomDialogView.findViewById(R.id.cancel);

        //点击拍照
        takePhoto.setOnClickListener(vv -> {
            takePhoto(takePhotoLauncher);
            dialog.dismiss();
        });

        //点击相册
        selectPhoto.setOnClickListener(vv -> {
            selectPhoto(selectPhotoLauncher);
            dialog.dismiss();
        });

        //点击取消 弹窗消失
        cancel.setOnClickListener(vv -> dialog.dismiss());

        dialog.setContentView(bottomDialogView);
        dialog.show();
    }

    //拍照并换返回
    public void takePhoto(ActivityResultLauncher<Uri> takePhotoLauncher) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "IMG_" + System.currentTimeMillis() + ".jpg");
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
        uri = requireActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        takePhotoLauncher.launch(uri);
    }

    //选择图片
    public void selectPhoto(ActivityResultLauncher<PickVisualMediaRequest> selectPhotoLauncher) {
        selectPhotoLauncher.launch(new PickVisualMediaRequest.Builder()
                .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                .build());
    }

    public App getApp() {
        return (App) getActivity().getApplication();
    }


}
